﻿using DataService.Common;
using DataService.Core.Log;
using DataService.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace DataService.DBSql
{
    public class SqlserverSql : DBSchemaSql
    {
        public override string GetSelectScalarCountSql<T>(Expression<Func<T, bool>> whereFunc, ref object paramValue, string fullTableName, string databaseName)
        {
            //超过百万数据时,count(1)会变吗，数据量越大count越慢,先这么查，优化阶段，从索引表查询数量
            //查询总行数
            //SELECT a.name,b.rows FROM  sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id WHERE a.xtype='U' AND b.indid=1
            //select rows from sysindexes where id = object_id('TB_Construction_Img') and indid in (0,1)
            ResolveExpress resolve = new ResolveExpress();
            var dic = resolve.ResolveExpression<T>(whereFunc, ParameterPrefix);
            if (!dic.ContainsKey("sql"))
            {
                throw new Exception("GetSelectSql解析参数错误");
            }
            string sql = $"SELECT Count(1) FROM {fullTableName} WHERE {dic["sql"].ToString()}";
            paramValue = dic["values"];
            TraceLog.WriteLine($"sqlserver执行{sql}", "执行语句");
            return sql;
        }

    }
}
